package com.xmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xmall.bean.bo.CreateRoleBo;
import com.xmall.bean.bo.PermissionBo;
import com.xmall.bean.bo.RoleListBo;
import com.xmall.bean.table.Permission;
import com.xmall.bean.table.Role;
import com.xmall.bean.table.RoleExample;
import com.xmall.bean.vo.ListDataVo;
import com.xmall.bean.vo.permission.PermissionsVo;
import com.xmall.bean.vo.RoleOptionListVo;
import com.xmall.bean.vo.permission.SystemPermissionsBean;
import com.xmall.mapper.PermissionMapper;
import com.xmall.mapper.RoleMapper;
import com.xmall.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public ListDataVo<Role> queryRoleList(RoleListBo roleListBo) {
        PageHelper.startPage(roleListBo.getPage(), roleListBo.getLimit());
        RoleExample roleExample = new RoleExample();
        roleExample.setOrderByClause(roleListBo.getSort() + " " + roleListBo.getOrder());
        RoleExample.Criteria criteria = roleExample.createCriteria();
        if (roleListBo.getName() != null) {
            criteria.andNameLike("%" + roleListBo.getName() + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<Role> roles = roleMapper.selectByExample(roleExample);
        PageInfo<Role> rolePageInfo = new PageInfo<>(roles);
        int  total = (int) rolePageInfo.getTotal();
        return new ListDataVo<Role>(roles, total);
    }

    @Override
    public Role createRole(CreateRoleBo createRoleBo) {
        Role role = new Role(null, createRoleBo.getName(), createRoleBo.getDesc(), true, new Date(), new Date(), false);
        roleMapper.insertSelective(role);
        Role roleVo = roleMapper.selectByPrimaryKey(role.getId());
        return roleVo;
    }

    @Override
    public void deleteRole(Role role) {
        role.setEnabled(false);
        role.setDeleted(true);
        String s = UUID.randomUUID().toString();
        role.setName(s + role.getName());
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andIdEqualTo(role.getId());
        roleMapper.updateByExampleSelective(role, roleExample);
    }

    @Override
    public void updateRole(Role role) {
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andIdEqualTo(role.getId());
        roleMapper.updateByExampleSelective(role, roleExample);
    }

    @Override
    public List<RoleOptionListVo> options() {
        List<RoleOptionListVo> roleOptionListVos = roleMapper.selectAllRoles();
        return roleOptionListVos;
    }

    @Override
    public void authorization(PermissionBo permissionBo) {
        List<String> permissions = permissionBo.getPermissions();
        for (String permission : permissions) {
            Permission insertPermission = new Permission(null, permissionBo.getRoleId(), permission, new Date(), new Date(), false);
            permissionMapper.insert(insertPermission);
        }
    }

    @Override
    public PermissionsVo getAllPermissions(Integer roleId) {
        List<SystemPermissionsBean> systemPermissionsBeans = roleMapper.queryAllPermission();
        List<String> assignedPermissions = roleMapper.queryAssignedPermissions(roleId);
        return new PermissionsVo(systemPermissionsBeans, assignedPermissions);
    }


}
